<?PHP	//header
include('config/config.php');
include('php/header.php');
require "php/functions.php";
?>        

<?PHP	//Connect to database
	$conn = mysql_connect($cfg["connection"]["host"], $cfg["connection"]["userName"], $cfg["connection"]["password"]) 
		or die ('Error Cannot Connect to MySQL');
	@mysql_select_db($cfg["connection"]["database"]) or die( "Unable to select database");
?>
    
<?PHP	//pagination; for displaying main window's contain
		
		$tbl_name=$cfg["tbl"]["shopLocation"];	//your table name
		/* Setup vars for query. */
		// How many adjacent pages should be shown on each side?
		$adjacents = 3;
		$targetpage = "region.php"; 			//your file name  (the name of this file)
		$limit = 10; 						//how many items to show per page
		if (isset($_GET['page'])) $page = $_GET['page'];
		else $page = 1;
		if ($page) $start = ($page - 1) * $limit; 			//first item to display on this page
		else $start = 0;									//if no page var is given, set start to 0
		
		/* Get data. */
		
		session_start();
		
		if (isset($_SESSION['checkbox_shopLocation']) && is_array($_SESSION['checkbox_shopLocation'])) {
//			println($_POST['checkbox_shopLocation'][0]);
			$column_name = $cfg["tbl_shopLocation"]["shopID"];
			$column_targetVal = $_SESSION['checkbox_shopLocation'][0];
			$sql = "SELECT * FROM $tbl_name WHERE $column_name = $column_targetVal";
			$result = mysql_query($sql) or die (mysql_error());
			
			$row = mysql_fetch_array($result);
			$column_name = $cfg["tbl_shopLocation"]["location"];
			$column_targetVal = $row[$cfg["tbl_shopLocation"]["location"]];
			$sql = "SELECT * FROM $tbl_name WHERE $tbl_name.$column_name ="." \"".$column_targetVal."\" LIMIT $start, $limit";
			$result = mysql_query($sql);
			
			/* 
			   First get total number of rows in data table. 
			   If you have a WHERE clause in your query, make sure you mirror it here.
			*/
			$query = "SELECT COUNT(*) as num FROM (SELECT * FROM $tbl_name WHERE $column_name ="." \"".$column_targetVal."\"".") AS alias;";
			$total_items = mysql_fetch_array(mysql_query($query));
			$total_items = $total_items['num'];
			
			include('config/pagination.php');
		}
?>
<h2>Shops Locate in <?PHP echo "$column_targetVal" ?></h2> 
                
<table id="rounded-corner" summary="Shop Database">
    <thead>
    	<tr>
			<th scope="col" class="rounded-company"></th>
            <th scope="col" class="rounded">Shop ID</th>
            <th scope="col" class="rounded">Shop Location</th>
            <th scope="col" class="rounded-q4">Shop Address</th>
        </tr>
    </thead>
        <tfoot>
    	<tr>
        	<td colspan="3" class="rounded-foot-left"><em>Shop Location</em></td>
        	<td class="rounded-foot-right">&nbsp;</td>

        </tr>
    </tfoot>
	
    <tbody>
	<?PHP	//output from database to main window's box
	echo '<div class="divOuter">';		//parent div ----- just to format multiple children divs inline
		echo '<form method="POST" action="buttonRedirectShop.php">';	
			while($row = mysql_fetch_array($result)){
				echo "<tr>";
					echo "<td><input type=\"checkbox\" name=\"checkbox_shopLocation[]\" value='".$row[$cfg["tbl_shopLocation"]["shopID"]]."'/></td>";
					echo "<td>".$row[$cfg["tbl_shopLocation"]["shopID"]]."</td>";
					echo "<td>".$row[$cfg["tbl_shopLocation"]["location"]]."</td>";
					echo "<td>".$row[$cfg["tbl_shopLocation"]['address']]."</td>";
				echo "</tr>";
			}
	echo '</div>';
	?>
	</tbody>
</table>
	<?PHP
	echo '<div class="divOuter">';	
			//buttons		
			echo '<div id="deleteForm" style="display:inline-block" align="right" >';
				echo '<input type="submit" name="action" value="Delete Shops"/>'; 
			echo '</div>';

			echo '<div id="addForm" style="display:inline-block" align="right" >';
				echo '<input type="submit" name="action" value="Update Shop Information"/>';
			echo '</div>';  
			
			echo '<div id="viewCatForm" style="display:inline-block" align="right" >';
				echo '<input type="submit" name="action" value="View Shops in Region"/>';
			echo '</div>';  
			
		echo '</form>'; 
	echo '</div>';
	?>

     
	<div class="pagination">
	<?PHP	
		echo $pagination
	?>
<?PHP	//footer
include('php/footer.php');
?>
<?PHP
		// if (isset($_POST['checkbox_shopLocation']) && is_array($_POST['checkbox_shopLocation']) ){
			// foreach($_POST['checkbox_shopLocation'] as $inBarcode)
				// echo "<tr>".$inBarcode."</tr>";
		// }
?>